package com.jn.book.views;

import android.content.Intent;
import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.jn.book.R;
import com.jn.book.adapter.CatalogAdapter;
import com.jn.book.base.BaseActivity;
import com.jn.book.databinding.ActivityCatalogBinding;
import com.jn.book.entity.CatalogEntity;
import com.jn.book.viewmodel.CatalogViewModel;

import java.util.ArrayList;
import java.util.List;

public class CatalogActivity extends BaseActivity<ActivityCatalogBinding> {

    private CatalogAdapter mAdapter;
    private List<CatalogEntity.ResultDTO> mList;

    @Override
    public int getLayoutId() {
        return R.layout.activity_catalog;
    }

    @Override
    public void init() {
        CatalogViewModel catalogViewModel = new ViewModelProvider(this).get(CatalogViewModel.class);
        LiveData<CatalogEntity> catalogLiveData = catalogViewModel.getLiveData();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mBinding.recyclerView.setLayoutManager(linearLayoutManager);
        mList = new ArrayList<>();
        mAdapter = new CatalogAdapter(mList);
        mBinding.recyclerView.setAdapter(mAdapter);
        catalogLiveData.observe(this, new Observer<CatalogEntity>() {
            @Override
            public void onChanged(CatalogEntity catalog) {
                mList = catalog.getResult();
                mAdapter.updateData(mList);
            }
        });
        mAdapter.setOnItemClickListener(new CatalogAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(String catalog_id) {
                Log.d("asdf", "===" + catalog_id);
                Intent intent = new Intent(CatalogActivity.this, BooksActivity.class);
                intent.putExtra("catalog_id", catalog_id);
                startActivity(intent);
            }
        });
        catalogViewModel.getData("39874cea287f683d8ef362984c4de76e");
    }
}